package com.wavecoler.professor.dao;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.wavecoler.dao.PMF;
import com.wavecoler.professor.bots.BotERProblems;
import com.wavecoler.professor.objects.Problem;
import com.wavecoler.professor.objects.User;

import java.util.*;

import javax.jdo.Query;
import java.util.logging.Logger;

public class ProblemDAO {
	private static final Logger log = Logger.getLogger(ProblemDAO.class
			.getName());

	// Method to insert a new user
	public static void insertProblem(Problem problem) {

		// LLamamos a la clase que tendra la persistencia
		PersistenceManager pm = null;

		// Haciendo la persistencia de datos
		pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(problem);
			tx.commit();
			log.info("transaction commit");
		} catch (Exception e) {
			log.warning(e.getMessage());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
				log.warning("did transaction rollback");
			}
			if (pm != null)
				pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	// Method to return the list of users
	public static List<Problem> getProblemList() {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select from " + Problem.class.getName()
					+ "");
			List<Problem> problems = (List<Problem>) query.execute();
			problems.size();
			log.info("Finished retrieving problems");
			log.warning("Finished retrieving problems");
			return problems;
		} catch (Exception e) {
			log.info(e.getMessage() + " -- " + e.getCause());
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
	}

	public static List<String> getProblemNames() {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select Name from "
					+ Problem.class.getName() + "");
			List<String> problems = (List<String>) query.execute();
			problems.size(); // es necesario agregarlo debido a bug -
								// http://www.atentia.net/2010/03/app-engine-and-jdo-object-manager-has-been-closed/
			log.info("Finished retrieving problem names");
			log.warning("Finished retrieving problem names");
			return problems;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
	}
	
	public static String getProblemDescription(String problemname) {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select Description from "
					+ Problem.class.getName() + " where Name == :p1");
			List<String> problemDescriptions = (List<String>) query.execute(problemname);
			problemDescriptions.size();
			log.info("Finished retrieving problem description");
			log.warning("Finished retrieving problem description");
			return problemDescriptions.get(0);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}
	}

	public static Long getProblemID(String problemName) {
		PersistenceManager pm = null;
		try {
			pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery("select ProblemId from "
					+ Problem.class.getName() + " where Name == :p1");
			List<Long> problems = (List<Long>) query.execute(problemName);
			problems.size(); // es necesario agregarlo debido a bug -
								// http://www.atentia.net/2010/03/app-engine-and-jdo-object-manager-has-been-closed/
			log.info("Finished retrieving problem ID");
			log.warning("Finished retrieving problem ID");
			return problems.get(0);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (pm != null)
				pm.close();
		}

	}

}